<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="VSCode调试C/C++项目：配置tasks.json和launch.json, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="本文介绍了使用VSCode来调试C/C++工程的第一步：配置tasks.json和launch.json">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>VSCode调试C/C++项目：配置tasks.json和launch.json | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011012250590.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">VSCode调试C/C++项目：配置tasks.json和launch.json</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/%E6%9D%82%E9%A1%B9/">
                                <span class="chip bg-color">杂项</span>
                            </a>
                        
                            <a href="/tags/VSCode/">
                                <span class="chip bg-color">VSCode</span>
                            </a>
                        
                            <a href="/tags/tasks-json/">
                                <span class="chip bg-color">tasks.json</span>
                            </a>
                        
                            <a href="/tags/launch-json/">
                                <span class="chip bg-color">launch.json</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/VSCode%E8%B0%83%E8%AF%95C-C/" class="post-category">
                                VSCode调试C/C++
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-10-11
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    6.5k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    28 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011012250590.png" alt="使用VSCode来调试C/C++工程：配置tasks.json和launch.json"></p>
<h1 id="VSCode调试C-C-项目"><a href="#VSCode调试C-C-项目" class="headerlink" title="VSCode调试C/C++项目"></a>VSCode调试C/C++项目</h1><p>VS Code作为宇宙第一编辑器，在众多插件的加持下，具有了调试、单元测试等等功能，使其越来越像一个IDE。</p>
<p>然而很多人其实并不会使用VS Code的调试功能，只是把VS Code当做了一个带有语法补全的编辑器。这实际上极大地浪费了VS Code的功能，尤其是对于C/C++开发者来说，使用命令行的GDB调试远不如使用VS Code内嵌的GDB图形化界面调试来的舒服。</p>
<p>本文就讲介绍如何使用VS Code调试C/C++项目</p>
<h2 id="1-概述"><a href="#1-概述" class="headerlink" title="1. 概述"></a>1. 概述</h2><p>通常我们在调试一个C/C++工程的时候，大体上的流程可以分为两步：</p>
<ul>
<li><strong>启动调试器（GDB）前的准备工作</strong></li>
<li><strong>启动调试器（GDB）进行调试</strong></li>
</ul>
<p>例如对于一个<code>CMake</code>组织的C/C++项目，这两大步具体包含的流程如下（编写<code>CMakeLists.txt</code>是在编码阶段，编码是与调试独立的阶段）：</p>
<ul>
<li><strong>启动调试器（GDB）前的准备工作</strong><ol>
<li>创建<code>build</code>文件夹：<code>mkdir -p build</code></li>
<li>切换到<code>build</code>文件夹：<code>cd build</code></li>
<li>配置（Configure）项目：<code>cmake .. &lt;option&gt;</code></li>
<li>构建/编译（Build）项目：<code>make</code></li>
</ol>
</li>
<li><strong>启动调试器（GDB）进行调试</strong><ol>
<li>启动调试器：<code>gdb &lt;path-to-executable</code></li>
</ol>
</li>
</ul>
<p>对于不同的项目（<code>npm</code>项目、<code>C#</code>项目、<code>java</code>项目等等），可能在启动调试器前的准备工作不同，但是大体上都可以分为进行调试前需要进行的一系列任务，以及结合具体参数启动时调试器</p>
<p>因此，对于这两个阶段，VSCode中提供了<code>tasks.json</code>和<code>launch.json</code>两个文件来分别描述<code>调试前的准备工作</code>以及<code>以指定的参数启动调试器</code></p>
<h2 id="2-调试前的准备工作：tasks-json"><a href="#2-调试前的准备工作：tasks-json" class="headerlink" title="2. 调试前的准备工作：tasks.json"></a>2. 调试前的准备工作：tasks.json</h2><p>VSCode使用<code>tasks.json</code>来描述启动调试前的准备工作。</p>
<h3 id="A-tasks-json的结构"><a href="#A-tasks-json的结构" class="headerlink" title="A. tasks.json的结构"></a>A. tasks.json的结构</h3><p><code>tasks.json</code>的结构一般如下</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>

    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"inputs"</span><span class="token operator">:</span> <span class="token punctuation">[</span>

    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011021938530.png" alt="tasks.json的一般结构"></p>
<h3 id="B-version标签"><a href="#B-version标签" class="headerlink" title="B.  version标签"></a>B.  version标签</h3><p><code>version</code>标签指定了<code>Tasks.json</code>的版本，因为不同的版本支持的标签不一样，所以需要使用<code>version</code>标签指明版本。</p>
<p>目前<code>version</code>支持<code>2.0.0</code>版本，所以直接指定<code>version</code>为<code>2.0.0</code>即可。</p>
<h3 id="C-tasks标签"><a href="#C-tasks标签" class="headerlink" title="C. tasks标签"></a>C. tasks标签</h3><p><code>tasks</code>标签是一个列表，我们在其中定义不同的<code>task</code>，而关于具体的<code>task</code>如何定义则见下</p>
<p>我们以创建<code>build</code>文件夹这个任务为例</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
        <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"create dir"</span><span class="token punctuation">,</span>
        <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
        <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"mkdir"</span><span class="token punctuation">,</span>
        <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token string">"-p"</span><span class="token punctuation">,</span>
            <span class="token string">"build"</span>
        <span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-Force"</span><span class="token punctuation">,</span>
                <span class="token string">"build"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"shell"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"executable"</span><span class="token operator">:</span> <span class="token string">"powershell.exe"</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="1-label标签"><a href="#1-label标签" class="headerlink" title="1) label标签"></a>1) label标签</h4><p><code>label</code>标签定义了一个任务的名字，稍后我们能用通过名字取定位一个任务，从而实现诸如将多个任务合并为一个组，而后执行一组任务这样的操作。</p>
<p><code>label</code>标签的值是随我们自己喜欢，想写什么就写什么的。</p>
<h4 id="2-type标签"><a href="#2-type标签" class="headerlink" title="2) type标签"></a>2) type标签</h4><p><code>type</code>标签指定了一个任务的类型。所有的任务大致上可以分为两类：</p>
<ul>
<li>第一类就是在<code>Shell</code>中执行的命令，值为<code>shell</code></li>
<li>第二类就是一个进程，例如我们写的程序是操作<code>MySQL</code>数据库的程序，那么就需要在调试前启动<code>MySQL</code>数据库，则此时<code>MySQL</code>数据库就是进程形式的任务。进程形式的任务的值为<code>process</code></li>
</ul>
<h4 id="3-command标签"><a href="#3-command标签" class="headerlink" title="3 ) command标签"></a>3 ) command标签</h4><p><code>command</code>标签指定了需要执行的命令或者程序。</p>
<ul>
<li>如果是<code>Shell</code>中的命令的话，那么<code>command</code>的值为需要执行的命令。</li>
<li>如果是进程的话，那么<code>command</code>的值为需要执行的可执行程序的位置，可执行程序可以是有x权限的<code>.sh</code>，也可以是<code>.exe</code>等可执行程序。</li>
</ul>
<h4 id="4-args标签"><a href="#4-args标签" class="headerlink" title="4 ) args标签"></a>4 ) args标签</h4><p><code>args</code>标签指定了执行的命令或者程序时传入的命令行参数。在具体执行的时候会把多个参数用空格连接起来而后执行。</p>
<p>结合<code>command</code>标签，我们执行的命令就是下面这句话</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">mkdir</span> -p build
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h4 id="5-windows标签"><a href="#5-windows标签" class="headerlink" title="5 ) windows标签"></a>5 ) windows标签</h4><p><code>windows</code>标签指定了只有在<code>windows</code>系统上的配置。我们在<code>windows</code>标签中指定了两个标签<code>options</code>标签和<code>args</code>标签。</p>
<ul>
<li><p>对于<code>args</code>标签就意味着在其他系统（<code>Linux</code>/<code>MacOS</code>）上，使用<code>-p build</code>作为命令行参数，而在<code>Windows</code>系统上，使用<code>-Force build</code>作为命令行参数。</p>
<p>这是因为在<code>Linux</code>/<code>MacOS</code>系统上，创建一个文件夹使用下面的命令就行了</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">mkdir</span> -p 文件夹名
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>但是在<code>Windows</code>平台上，创建一个文件夹需要使用下面的命令</p>
<pre class="line-numbers language-powershell"><code class="language-powershell">mkdir <span class="token operator">-</span>Force 文件夹名
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
</li>
<li><p>对于<code>options</code>标签就意味着只有在<code>Windows</code>平台上才会有这个标签。</p>
</li>
</ul>
<h4 id="6-options标签"><a href="#6-options标签" class="headerlink" title="6 ) options标签"></a>6 ) options标签</h4><p><code>options</code>标签指明了运行命令的<code>shell</code>的位置（<code>shell</code>标签）、运行命令的环境变量（<code>env</code>标签）以及运行命令的文件夹（<code>cwd</code>标签）。当然这里只用了<code>shell</code>这一个标签。</p>
<p>使用<code>shell</code>标签的原因是因为在<code>Windows</code>上有两个命令行，一个是<code>cmd</code>一个是<code>powershell</code>。而<code>mkdir</code>这个命令是在<code>powershell</code>中的，因此我们需要特殊指明在<code>Windwos</code>上需要使用<code>powershell.exe</code>作为Shell的解释器</p>
<h3 id="D-input标签"><a href="#D-input标签" class="headerlink" title="D. input标签"></a>D. input标签</h3><p><code>input</code>标签用于生成一个选项卡，接收用户的输入，一般是和<code>args</code>标签一起使用我们稍后再讲解这个标签的用法。</p>
<h2 id="3-启动调试器：launch-json"><a href="#3-启动调试器：launch-json" class="headerlink" title="3. 启动调试器：launch.json"></a>3. 启动调试器：launch.json</h2><h3 id="A-launch-json的结构"><a href="#A-launch-json的结构" class="headerlink" title="A. launch.json的结构"></a>A. launch.json的结构</h3><p><code>launch.json</code>的结构一般如下</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问<span class="token operator">:</span> https<span class="token operator">:</span>//go.microsoft.com/fwlink/?linkid=<span class="token number">830387</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"0.2.0"</span><span class="token punctuation">,</span>
    <span class="token property">"configurations"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"gdb"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"launch"</span><span class="token punctuation">,</span>
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"GDB"</span><span class="token punctuation">,</span>
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/${command:AskForProgramName}"</span><span class="token punctuation">,</span>
            <span class="token property">"stopOnEntry"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
            <span class="token property">"arguments"</span><span class="token operator">:</span> <span class="token string">""</span>，
            <span class="token property">"preLaunchTask"</span><span class="token operator">:</span> <span class="token string">""</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011145556587.png" alt="launch.json的结构"></p>
<h3 id="B-version标签-1"><a href="#B-version标签-1" class="headerlink" title="B. version标签"></a>B. version标签</h3><p><code>launch.json</code>中的<code>version</code>标签和<code>tasks.json</code>中的<code>version</code>标签作用是一样的，一般都用<code>0.2.0</code>。</p>
<h3 id="C-configuration标签"><a href="#C-configuration标签" class="headerlink" title="C. configuration标签"></a>C. configuration标签</h3><p><code>configuration</code>标签中定义了开始启动调试器时候的具体的配置信息。具体来说，可以有多套配置信息。即<code>configuration</code>标签下可以有多个条目。</p>
<h4 id="1-name标签"><a href="#1-name标签" class="headerlink" title="1 ) name标签"></a>1 ) name标签</h4><p><code>name</code>标签定义了一套配置信息的名称，这个名称稍后可以在左边的<code>运行与调试</code>页面中看到。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011155916661.png" alt="name定义调试的配置名称，可以在运行与调试界面选择需要运行的配置"></p>
<h4 id="2-type标签-1"><a href="#2-type标签-1" class="headerlink" title="2 ) type标签"></a>2 ) type标签</h4><p><code>type</code>标签指定了调试时启动的调试器：</p>
<ul>
<li><p>对于<code>C/C++</code>项目来说，<code>type</code>的值指定为<code>cppdbg</code>或者是<code>cppvsdbg</code></p>
<ul>
<li>在<code>Windows</code>上开发一般用的编译器都是<code>Visual Studio</code>中自带的<code>msvc</code>编译器，适用的调试器也是<code>Visual Studio</code>自带的，此时就需要把值设为<code>cppvsdbg</code></li>
<li><code>`Linux</code>上用的一般都是<code>gcc</code>，<code>MacOS</code>上用的编译器一般都是<code>clang</code>，对应的调试器分别是<code>gdb</code>和<code>lldb</code>，此时需要把值设为`cppdbg</li>
</ul>
</li>
<li><p>对于<code>Python</code>项目来说，<code>type</code>的值指定为<code>python</code>，因为<code>python</code>解释器自带了<code>pdb</code>这个调试器</p>
</li>
<li>…</li>
</ul>
<p>剩下的具体查询手册：<a target="_blank" rel="noopener" href="https://code.visualstudio.com/docs">https://code.visualstudio.com/docs</a></p>
<h4 id="3-request标签"><a href="#3-request标签" class="headerlink" title="3 ) request标签"></a>3 ) request标签</h4><p><code>request</code>标签指明了调试器调试程序的方式。具体来说有两种：</p>
<ul>
<li><code>launch</code>：表示调试器直接启动程序进行调试，类似于使用命令<code>gdb helloworld</code>，将会直接运行命令<code>helloworld</code></li>
<li><code>attach</code>：有时候，我们需要调试的程序运行在远程服务器上，此时在服务器上已经运行了一个<code>gdb</code>，而且服务器上的<code>gdb</code>把调试服务暴露在某一个端口上，此时我们在本机上运行<code>gdb</code>的时候，通过链接远程服务器该端口，从而实现用本地的<code>gdb</code>调试远程服务器上的程序。<strong>此时，远程服务器上的<code>gdb</code>称为<code>gdb server</code></strong>。这种调试方式称为<code>attach</code>，即把调试器附加到一个<code>gdb server</code>上去。</li>
</ul>
<p>一般在本机做调试的时候值都是<code>launch</code>。</p>
<h4 id="4-program标签"><a href="#4-program标签" class="headerlink" title="4 ) program标签"></a>4 ) program标签</h4><p><code>program</code>标签指定了我们需要调试的程序。注意，如果<code>request</code>标签的值是<code>attach</code>的话，那么就不能使用<code>program</code>标签。</p>
<h4 id="5-workspaceFolder宏"><a href="#5-workspaceFolder宏" class="headerlink" title="5 ) workspaceFolder宏"></a>5 ) workspaceFolder宏</h4><p><code>CMake</code>中有<code>EXECUTABLE_OUTPUT_PATH</code>宏，我们可以指定<code>EXECUTABLE_OUTPUT_PATH</code>宏的值从而指定可执行文件输出的路径，也可以通过<code>${}</code>来读取<code>EXECUTABLE_OUTPUT_PATH</code>宏的值来打印到屏幕上或者用于为其他宏赋值。</p>
<p>类似的，<code>VSCode</code>中也有功能类似的宏，<code>workspaceFolder</code>这个宏就表示了当前打开的目录。我们也可以使用<code>${}</code>来获取这个宏的值。</p>
<h4 id="6-command-AskForProgramName"><a href="#6-command-AskForProgramName" class="headerlink" title="6 ) command:AskForProgramName"></a>6 ) command:AskForProgramName</h4><p><code>command:AskForProgramName</code>这个宏的作用就是在程序运行的时候在上面弹出来一个选项卡，询问用户需要调试的程序的名字。</p>
<p>例如我们直接对着<code>launch.json</code>这个程序按下<code>F5</code>，然后就会弹出来一个选项卡让我们输入需要调试的程序的名字</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011155802472.png" alt="VSCode弹出选项卡要求用户输入程序的名字"></p>
<h4 id="7-stopAtEntry标签"><a href="#7-stopAtEntry标签" class="headerlink" title="7 ) stopAtEntry标签"></a>7 ) stopAtEntry标签</h4><p><code>stopAtEntry</code>标签表示在进入到主程序之后就会停下来，对于<code>C/C++</code>来说就是在进入<code>main</code>之后就停下来。</p>
<p>但是一般我们都是打上断点，然后直接运行到断点处，所以这个<code>stopAtEntry</code>的值一般用的都是<code>false</code>。</p>
<h4 id="8-Arguments标签"><a href="#8-Arguments标签" class="headerlink" title="8 ) Arguments标签"></a>8 ) Arguments标签</h4><p>这个标签我没用过，所以我也搞不清楚，如果要传参的给程序的话，用<code>args</code>标签</p>
<h4 id="9-preLaunchTask标签"><a href="#9-preLaunchTask标签" class="headerlink" title="9 ) preLaunchTask标签"></a>9 ) preLaunchTask标签</h4><p><code>preLaunchTask</code>标签可以说是最重要的标签之一，它沟通了<code>launch.json</code>和<code>tasks.json</code>这两个文件。</p>
<p>前面我们在<code>tasks.json</code>中定义了一系列任务，而<code>launch.json</code>中的这个标签说明了在启动调试器前需要执行的<code>tasks.json</code>中的那个任务。</p>
<p>所以利用这个标签，我们就可以实现从调试前的准备工作再到启动调试器这一连串的任务。</p>
<h2 id="4-一个Toy-Example-echo-宏"><a href="#4-一个Toy-Example-echo-宏" class="headerlink" title="4. 一个Toy Example: echo 宏"></a>4. 一个Toy Example: echo 宏</h2><p>下面展示一个Toy Example来展示<code>tasks.json</code>和<code>launch.json</code>的workflow</p>
<h3 id="A-tasks-json的内容"><a href="#A-tasks-json的内容" class="headerlink" title="A. tasks.json的内容"></a>A. tasks.json的内容</h3><p>Toy Example中<code>tasks.json</code>的内容如下</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"example"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"echo"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"${file}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileBasename}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileBasenameNoExtension}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileDirname}\n"</span><span class="token punctuation">,</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>具体来说我们就是想要执行一下下面的命令</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token keyword">echo</span> <span class="token string">"<span class="token variable">${file}</span>\n"</span> <span class="token string">"<span class="token variable">${fileBasename}</span>\n"</span> <span class="token string">"<span class="token variable">${fileBasenameNoExtension}</span>\n"</span> <span class="token string">"<span class="token variable">${fileDirname}</span>\n"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>主要是看一看这四个宏的值分别是什么</p>
<h3 id="B-launch-json的内容"><a href="#B-launch-json的内容" class="headerlink" title="B. launch.json的内容"></a>B. launch.json的内容</h3><p>Toy Example中<code>lauch.json</code>的内容如下</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问: https://go.microsoft.com/fwlink/?linkid<span class="token operator">=</span>830387
    <span class="token string">"version"</span><span class="token keyword">:</span> <span class="token string">"0.2.0"</span>,
    <span class="token string">"configurations"</span><span class="token keyword">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token keyword">:</span> <span class="token string">"gdb"</span>,
            <span class="token string">"request"</span><span class="token keyword">:</span> <span class="token string">"launch"</span>,
            <span class="token string">"name"</span><span class="token keyword">:</span> <span class="token string">"Toy Example"</span>,
            <span class="token string">"program"</span><span class="token keyword">:</span> <span class="token string">"<span class="token variable">${workspaceFolder}</span>/<span class="token variable">${file}</span>"</span>,
            <span class="token string">"stopOnEntry"</span><span class="token keyword">:</span> true,
            <span class="token string">"preLaunchTask"</span><span class="token keyword">:</span> <span class="token string">"example"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>具体来说就是在启动调试器之前运行一下上面定义的<code>example</code>这个task。</p>
<h3 id="C-hello-world-c"><a href="#C-hello-world-c" class="headerlink" title="C. hello_world.c"></a>C. hello_world.c</h3><p>我们接下来写一个<code>hello_world.c</code>，里面的内容如下：</p>
<pre class="line-numbers language-c"><code class="language-c"><span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h></span></span>


<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> argc<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s\n"</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Hello World!\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="D-开始调试"><a href="#D-开始调试" class="headerlink" title="D. 开始调试"></a>D. 开始调试</h3><p>首先在<code>运行和调试</code>界面把调试的配置选定为<code>Toy Example</code>，然后编辑器打开<code>hello_world.c</code></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011163456116.png" alt="开始调试"></p>
<p>接下来按<code>F5</code>开始调试</p>
<p>此时我们在终端就能够看到执行的任务以及输出</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011163622369.png" alt="执行的任务以及输出"></p>
<p>很清楚就能看到，上面四个宏的值分别是</p>
<pre class="line-numbers language-json"><code class="language-json">$<span class="token punctuation">{</span>file<span class="token punctuation">}</span>                                                    <span class="token operator">:</span>        /Users/jack/project/test/vscode_test/hello_world.c
$<span class="token punctuation">{</span>fileBasename<span class="token punctuation">}</span>                                    <span class="token operator">:</span>        hello_world.c
$<span class="token punctuation">{</span>fileBasenameNoExtension<span class="token punctuation">}</span>            <span class="token operator">:</span>        hello_world
$<span class="token punctuation">{</span>fileDirname<span class="token punctuation">}</span>                                    <span class="token operator">:</span>        /Users/jack/project/test/vscode_test
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="5-一个Toy-Example：编译文件"><a href="#5-一个Toy-Example：编译文件" class="headerlink" title="5. 一个Toy Example：编译文件"></a>5. 一个Toy Example：编译文件</h2><p>我们对上面的Toy Example进行修改，增加一个自动编译的功能</p>
<h3 id="A-tasks-json的内容-1"><a href="#A-tasks-json的内容-1" class="headerlink" title="A. tasks.json的内容"></a>A. tasks.json的内容</h3><p>我们给<code>tasks.json</code>新加一个task，即自动编译，此外我们修改一下输出宏的task</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"echo"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"echo"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"${file}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${pathSeparator}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileBasenameNoExtension}\n"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileDirname}${pathSeparator}${fileBasenameNoExtension}.o\n"</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"build"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"gcc"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"${file}"</span><span class="token punctuation">,</span>
                <span class="token string">"-o"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileDirname}${pathSeparator}${fileBasenameNoExtension}.o"</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="B-launch-json的内容-1"><a href="#B-launch-json的内容-1" class="headerlink" title="B. launch.json的内容"></a>B. launch.json的内容</h3><p>我们再给launch.json中新加一个配置信息</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问<span class="token operator">:</span> https<span class="token operator">:</span>//go.microsoft.com/fwlink/?linkid=<span class="token number">830387</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"0.2.0"</span><span class="token punctuation">,</span>
    <span class="token property">"configurations"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"gdb"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"launch"</span><span class="token punctuation">,</span>
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Echo Macros"</span><span class="token punctuation">,</span>
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/${file}"</span><span class="token punctuation">,</span>
            <span class="token property">"stopOnEntry"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
            <span class="token property">"preLaunchTask"</span><span class="token operator">:</span> <span class="token string">"echo"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"gdb"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"launch"</span><span class="token punctuation">,</span>
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Gcc Compile"</span><span class="token punctuation">,</span>
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}${pathSeparator}${fileBasenameNoExtension}.o"</span><span class="token punctuation">,</span>
            <span class="token property">"stopAtEntry"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            <span class="token property">"preLaunchTask"</span><span class="token operator">:</span> <span class="token string">"build"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="C-运行Echo-Macros"><a href="#C-运行Echo-Macros" class="headerlink" title="C. 运行Echo Macros"></a>C. 运行Echo Macros</h3><p>首先输出一下在<code>build</code>这个task中使用到的宏。</p>
<p>具体来说在<code>运行和调试</code>界面选择配置为<code>Echo Macros</code></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011173622994.png" alt="选择配置为Echo Macros"></p>
<p>然后按下<code>F5</code>开始运行</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011173912976.png" alt="Echo Macros运行的结果"></p>
<p>可以看到，上面四个宏的值是</p>
<pre class="line-numbers language-json"><code class="language-json">$<span class="token punctuation">{</span>file<span class="token punctuation">}</span>                                                                                                                <span class="token operator">:</span>        /Users/jack/project/test/vscode_test/hello_world.c
$<span class="token punctuation">{</span>pathSeparator<span class="token punctuation">}</span>                                                                                            <span class="token operator">:</span>        /
$<span class="token punctuation">{</span>fileBasenameNoExtension<span class="token punctuation">}</span>                                                                        <span class="token operator">:</span>        hello_world
$<span class="token punctuation">{</span>fileDirname<span class="token punctuation">}</span>$<span class="token punctuation">{</span>pathSeparator<span class="token punctuation">}</span>$<span class="token punctuation">{</span>fileBasenameNoExtension<span class="token punctuation">}</span>.o        <span class="token operator">:</span>        /Users/jack/project/test/vscode_test/hello_world.o
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="D-运行Gcc-Compile"><a href="#D-运行Gcc-Compile" class="headerlink" title="D. 运行Gcc Compile"></a>D. 运行Gcc Compile</h3><p>接下来我们运行<code>Gcc Compile</code>，类似的，还是先在<code>运行和调试</code>界面选择<code>Gcc Compile</code>，然后按下<code>F5</code>开始运行</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011174815269.png" alt="选择配置为Gcc Compile"></p>
<p>而后我们就会发现在文件夹下就出现了编译后的文件</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011174929291.png" alt="Gcc Compile运行的结果"></p>
<h2 id="6-一个Toy-Example：调试程序"><a href="#6-一个Toy-Example：调试程序" class="headerlink" title="6. 一个Toy Example：调试程序"></a>6. 一个Toy Example：调试程序</h2><p>我们上面做到了编译程序，而在编译之后我们需要干的就是去调试这个程序。</p>
<p>首先需要明白的是，我们如果想要使用<code>gdb</code>、<code>lldb</code>等调试器去调试一个程序的时候，我们必须要在编译的时候指定<code>-g</code>参数，这样编译器（例如<code>gcc</code>、<code>clang</code>）在编译的时候就会把源代码、符号表等等信息写入到程序里面去。</p>
<p>而后在调试的时候，我们使用命令<code>gdb xxxx</code>/<code>lldb xxxx</code>，<code>gdb</code>/<code>lldb</code>就回去读取源代码和符号表，从而开始调试。</p>
<h3 id="A-tasks-json的内容-2"><a href="#A-tasks-json的内容-2" class="headerlink" title="A. tasks.json的内容"></a>A. tasks.json的内容</h3><p>我们首先新增加一个名为<code>debug_build</code>的task，具体来说就是在编译的时候加上<code>-g</code>参数</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"debug_build"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"gcc"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"${file}"</span><span class="token punctuation">,</span>
                <span class="token string">"-g"</span><span class="token punctuation">,</span>
                <span class="token string">"-o"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileDirname}${pathSeparator}${fileBasenameNoExtension}.o"</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="B-launch-json的内容-2"><a href="#B-launch-json的内容-2" class="headerlink" title="B. launch.json的内容"></a>B. launch.json的内容</h3><p>为了要进行debug，我们在launch.json中新加入一项，这一项可能会有些复杂</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问<span class="token operator">:</span> https<span class="token operator">:</span>//go.microsoft.com/fwlink/?linkid=<span class="token number">830387</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"0.2.0"</span><span class="token punctuation">,</span>
    <span class="token property">"configurations"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"cppdbg"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"launch"</span><span class="token punctuation">,</span>
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"LLDB Debug"</span><span class="token punctuation">,</span>
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}${pathSeparator}${fileBasenameNoExtension}.o"</span><span class="token punctuation">,</span>
            <span class="token property">"stopAtEntry"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            <span class="token property">"preLaunchTask"</span><span class="token operator">:</span> <span class="token string">"debug_build"</span><span class="token punctuation">,</span>
            <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}"</span><span class="token punctuation">,</span>
            <span class="token property">"MIMode"</span><span class="token operator">:</span> <span class="token string">"lldb"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>详细的解释如下：</p>
<ul>
<li><code>"type":"cppdbg"</code>：新加入的这一项的类型是<code>cppdgb</code>，表示<code>C/C++ Debug</code>。因为我们新添加的运行配置的目的就是给<code>C/C++</code>程序Debug，所以我们让这一项的类型是<code>cppdgb</code>。如果我们是别的项目的话，例如是<code>node.js</code>的项目的话，那么我们让这个运行配置的<code>type</code>是<code>node</code>即可</li>
<li><code>"cwd:"${workspaceFolder}"</code>：因为在开始调试的时候我们需要在指定的文件夹下运行调试器，所以就需要使用<code>cwd</code>标签指定工作目录，一般制定成项目的根目录，也就是<code>workspaceFolder</code>就行了</li>
<li><code>"MIMode":"lldb"</code>：不同的系统上使用的调试器不同，<code>MacOS</code>、<code>Linux</code>、<code>Windows</code>使用的调试器分别是<code>lldb</code>、<code>gdb</code>、<code>msvc</code>/<code>gdb</code>（<code>msvc</code>是<code>Visual Studio</code>带的调试器，<code>gdb</code>是<code>MinGW</code>带的调试器），所以我们需要使用<code>MIMode</code>标签指定使用的调试器的类型。</li>
</ul>
<p>此外，我们其实还可以使用<code>miDebuggerArgs</code>和<code>miDebuggerPath</code>来专门制定调用调试器时候传入的参数以及调试器的路径。</p>
<p>因为我写这篇博客时候用的是<code>Mac</code>，所以用的调试器就是<code>LLDB</code></p>
<h3 id="C-运行LLDB-Debug"><a href="#C-运行LLDB-Debug" class="headerlink" title="C. 运行LLDB Debug"></a>C. 运行LLDB Debug</h3><p>我们给前面的程序加上一个断点，然后选择运行配置为<code>LLDB Debug</code>，然后按下<code>F5</code>开始调试。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011184233211.png" alt="运行LLDB Debug"></p>
<p>接下来我们就进入了调试页面：</p>
<ul>
<li>下方：显示了所有的任务</li>
<li>左侧：显示了当前所有的变量以及变量的值、监视的变量以及表达式、函数的调用堆栈</li>
<li>中间：显式了正在调试的程序</li>
<li>上方：显式了调试的功能按钮</li>
</ul>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011184352703.png" alt="image-20221011184352703"></p>
<h2 id="7-一个Toy-Example：顺序执行"><a href="#7-一个Toy-Example：顺序执行" class="headerlink" title="7. 一个Toy Example：顺序执行"></a>7. 一个Toy Example：顺序执行</h2><p>我们上面调试了一个程序。但是在现实中，我们往往在调试前是需要顺序执行多个命令的，而不是简单的编译。</p>
<p>我们接下来给出的Toy Example在启动调试前就将顺序执行两步命令：</p>
<ul>
<li>创建一个<code>bin</code>文件夹</li>
<li>将编译好的源文件输出到<code>bin</code>文件夹中</li>
</ul>
<h3 id="A-tasks-json中的内容"><a href="#A-tasks-json中的内容" class="headerlink" title="A. tasks.json中的内容"></a>A. tasks.json中的内容</h3><p>我们在<code>tasks.json</code>中创建下面的两个任务</p>
<pre class="line-numbers language-json"><code class="language-json"><span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"create_bin"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"mkdir"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-p"</span><span class="token punctuation">,</span>
                <span class="token string">"${workspaceFolder}${pathSeparator}/bin"</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"debug_build"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"gcc"</span><span class="token punctuation">,</span>
            <span class="token property">"group"</span><span class="token operator">:</span> <span class="token string">"build"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"${file}"</span><span class="token punctuation">,</span>
                <span class="token string">"-g"</span><span class="token punctuation">,</span>
                <span class="token string">"-o"</span><span class="token punctuation">,</span>
                <span class="token string">"${fileDirname}${pathSeparator}bin${pathSeparator}${fileBasenameNoExtension}.o"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"dependsOn"</span><span class="token operator">:</span> <span class="token string">"create_bin"</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><code>create_bin</code>任务就是老三样，没啥好说的。</p>
<p>关键就在于修改之后的<code>debug_build</code>任务，<code>debug_build</code>任务中新增加了一个<code>dependsOn</code>标签，这个标签说明了在运行<code>debug_build</code>任务之前需要运行的任务。</p>
<p>在这里就表示在运行<code>debug_build</code>任务之前，需要运行<code>create_bin</code>任务。</p>
<h3 id="B-launch-json的内容-3"><a href="#B-launch-json的内容-3" class="headerlink" title="B. launch.json的内容"></a>B. launch.json的内容</h3><p><code>launch.json</code>中的内容保持不变，还是<code>LLDB Debug</code></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问: https://go.microsoft.com/fwlink/?linkid<span class="token operator">=</span>830387
    <span class="token string">"version"</span><span class="token keyword">:</span> <span class="token string">"0.2.0"</span>,
    <span class="token string">"configurations"</span><span class="token keyword">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token keyword">:</span> <span class="token string">"cppdbg"</span>,
            <span class="token string">"request"</span><span class="token keyword">:</span> <span class="token string">"launch"</span>,
            <span class="token string">"name"</span><span class="token keyword">:</span> <span class="token string">"LLDB Debug"</span>,
            <span class="token string">"program"</span><span class="token keyword">:</span> <span class="token string">"<span class="token variable">${workspaceFolder}</span><span class="token variable">${pathSeparator}</span>bin<span class="token variable">${pathSeparator}</span><span class="token variable">${fileBasenameNoExtension}</span>.o"</span>,
            <span class="token string">"stopAtEntry"</span><span class="token keyword">:</span> false,
            <span class="token string">"preLaunchTask"</span><span class="token keyword">:</span> <span class="token string">"debug_build"</span>,
            <span class="token string">"cwd"</span><span class="token keyword">:</span> <span class="token string">"<span class="token variable">${workspaceFolder}</span>"</span>,
            <span class="token string">"MIMode"</span><span class="token keyword">:</span> <span class="token string">"lldb"</span>,
        <span class="token punctuation">}</span>,
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="C-运行LLDB-Debug-1"><a href="#C-运行LLDB-Debug-1" class="headerlink" title="C. 运行LLDB Debug"></a>C. 运行LLDB Debug</h3><p>运行<code>LLDB Debug</code>的结果如下，可以发现首先<code>bin</code>文件被创建了，接着可执行文件输出到了<code>bin</code>文件夹中，而后开始debug</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221011222558619.png" alt="image-20221011222558619"></p>
<h2 id="8-一个真实的例子：CMake工程"><a href="#8-一个真实的例子：CMake工程" class="headerlink" title="8. 一个真实的例子：CMake工程"></a>8. 一个真实的例子：CMake工程</h2><p>我们上面讲了四个Toy Example，介绍了VSCode的<code>tasks.json</code>和<code>launch.json</code>最基本的功能，接下来我们就把这些功能结合到一起，用VSCode调试一个真实的<code>CMake</code>工程。</p>
<h3 id="A-CMake工程结构及文件"><a href="#A-CMake工程结构及文件" class="headerlink" title="A. CMake工程结构及文件"></a>A. CMake工程结构及文件</h3><p><code>CMake</code>工程的结构如下</p>
<pre class="line-numbers language-bash"><code class="language-bash">tree ./
./
├── CMakeLists.txt
├── main.c
├── wish.c
└── wish.h

0 directories, 4 files
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>项目的源文件一共有四个，其中：</p>
<ul>
<li><code>CMakeLists.txt</code>定义了项目结构</li>
<li><code>wish.c</code>和<code>wish.h</code>定义了<code>libwish</code>静态库</li>
<li><code>main.c</code>调用了<code>libwish</code>库</li>
</ul>
<h4 id="1-CMakeLists-txt"><a href="#1-CMakeLists-txt" class="headerlink" title="1 ) CMakeLists.txt"></a>1 ) CMakeLists.txt</h4><p><code>CMakeLists.txt</code>中的内容如下：</p>
<pre class="line-numbers language-cmake"><code class="language-cmake">project(WISH)

cmake_minimum_required(VERSION 3.9)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

add_library(
    libwish STATIC
    wish.c
)

add_executable(
    wish
    main.c
)

target_link_libraries(wish libwish)
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="2-wish-h-和-wish-c"><a href="#2-wish-h-和-wish-c" class="headerlink" title="2 ) wish.h 和 wish.c"></a>2 ) wish.h 和 wish.c</h4><pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// wish.h</span>

<span class="token macro property">#<span class="token directive keyword">ifndef</span> _WISH_H</span>

<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;string.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;stdbool.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;sys/wait.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h></span></span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h></span></span>

<span class="token macro property">#<span class="token directive keyword">define</span> _WISH_H 1</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_EXIT_SUCCESS 0</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_EXIT_FAILURE -1</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_BUF_SIZE 1024</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_MY_SEARCH 1</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_BUILTIN_NUM sizeof(builtin_str) / sizeof(char *)</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_MAX_WORD 20</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_MAX_FNAME 1024</span>
<span class="token macro property">#<span class="token directive keyword">define</span> WISH_MAX_PATH 128</span>

<span class="token macro property">#<span class="token directive keyword">define</span> WISH_DEBUG 1</span>

<span class="token comment" spellcheck="true">// Base Functions</span>
<span class="token keyword">void</span> <span class="token function">wish_loop</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                   <span class="token comment" spellcheck="true">// main loop of wish</span>
<span class="token keyword">char</span> <span class="token operator">*</span><span class="token function">wish_read_line</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                             <span class="token comment" spellcheck="true">// read user input line</span>
<span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token function">wish_split_line</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment" spellcheck="true">// split user input line into words</span>
<span class="token keyword">int</span> <span class="token function">wish_redirection</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                     <span class="token comment" spellcheck="true">// parse user input token to </span>
<span class="token keyword">int</span> <span class="token function">wish_execute</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">int</span> rarg_sht<span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment" spellcheck="true">// execute user input command</span>
<span class="token keyword">int</span> <span class="token function">wish_launch</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">int</span> rarg_sht<span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">// launch other program</span>
<span class="token keyword">char</span> <span class="token operator">*</span><span class="token function">wish_search</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>cmd<span class="token punctuation">)</span><span class="token punctuation">;</span>                           <span class="token comment" spellcheck="true">// search program in PATH</span>
<span class="token keyword">void</span> <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                                      <span class="token comment" spellcheck="true">// report error</span>
<span class="token keyword">void</span> <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">wish_cd</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">wish_exit</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">wish_path</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">wish_help</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">wish_env</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token macro property">#<span class="token directive keyword">endif</span></span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<pre class="line-numbers language-c"><code class="language-c"><span class="token comment" spellcheck="true">// wish.c</span>
<span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"wish.h"</span></span>

<span class="token comment" spellcheck="true">// path</span>
<span class="token keyword">char</span> <span class="token operator">*</span>path<span class="token punctuation">[</span>WISH_MAX_PATH<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">"/bin"</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// Shell Builtin Funtions</span>
<span class="token keyword">char</span> <span class="token operator">*</span>builtin_str<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">"cd"</span><span class="token punctuation">,</span>
    <span class="token string">"exit"</span><span class="token punctuation">,</span>
    <span class="token string">"path"</span><span class="token punctuation">,</span>
    <span class="token string">"help"</span><span class="token punctuation">,</span>
    <span class="token string">"wenv"</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>builtin_func<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token operator">&amp;</span>wish_cd<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>wish_exit<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>wish_path<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>wish_help<span class="token punctuation">,</span>
    <span class="token operator">&amp;</span>wish_env
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token keyword">void</span> <span class="token function">wish_loop</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>line<span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">;</span>
    bool status<span class="token punctuation">;</span>
    <span class="token keyword">do</span>
    <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"wish> "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        line <span class="token operator">=</span> <span class="token function">wish_read_line</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        args <span class="token operator">=</span> <span class="token function">wish_split_line</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token function">wish_redirection</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            args<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        status <span class="token operator">=</span> <span class="token function">wish_execute</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token function">free</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> WISH_MAX_WORD<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token function">free</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">free</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>status<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>status<span class="token punctuation">)</span>
        <span class="token function">exit</span><span class="token punctuation">(</span>WISH_EXIT_FAILURE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">char</span> <span class="token operator">*</span><span class="token function">wish_read_line</span><span class="token punctuation">(</span><span class="token keyword">void</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> position <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> bufsize <span class="token operator">=</span> WISH_BUF_SIZE<span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> buffer <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> bufsize<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> buffer<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">"wish: Memory allocation failed for read line.\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span>WISH_EXIT_FAILURE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">char</span> c<span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>true<span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// read a char</span>
        c <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>c <span class="token operator">==</span> <span class="token constant">EOF</span> <span class="token operator">||</span> c <span class="token operator">==</span> <span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            buffer<span class="token punctuation">[</span>position<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> buffer<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span>
            buffer<span class="token punctuation">[</span>position<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> c<span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">// resize buffer</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>position <span class="token operator">>=</span> bufsize<span class="token punctuation">)</span><span class="token punctuation">{</span>
            bufsize <span class="token operator">+</span><span class="token operator">=</span> WISH_BUF_SIZE<span class="token punctuation">;</span>
            <span class="token keyword">char</span> <span class="token operator">*</span> temp <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> bufsize<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> temp<span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">"wish: Memory allocation failed for read line.\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">exit</span><span class="token punctuation">(</span>EXIT_FAILURE<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment" spellcheck="true">// copy and reset pointer to new buffer</span>
            <span class="token keyword">int</span> num <span class="token operator">=</span> position<span class="token punctuation">;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>num <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                temp<span class="token punctuation">[</span>num<span class="token punctuation">]</span> <span class="token operator">=</span> buffer<span class="token punctuation">[</span>num<span class="token punctuation">]</span><span class="token punctuation">;</span>
                num<span class="token operator">--</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token function">free</span><span class="token punctuation">(</span>buffer<span class="token punctuation">)</span><span class="token punctuation">;</span>
            buffer <span class="token operator">=</span> temp<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>


<span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token function">wish_split_line</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>line<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>words <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token operator">*</span> WISH_MAX_WORD<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> WISH_MAX_WORD<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        words<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> k <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token function">strlen</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>temp <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        temp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> line<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>temp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">' '</span> <span class="token operator">||</span> temp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'\t'</span> <span class="token operator">||</span> temp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'\0'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            temp<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
            words<span class="token punctuation">[</span>k<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>i <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">strncpy</span><span class="token punctuation">(</span>words<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">,</span> temp<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            words<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
            j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> k <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> 
            j <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">free</span><span class="token punctuation">(</span>temp<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> words<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_redirection</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>args<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>args<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'>'</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> i<span class="token punctuation">;</span>
        i <span class="token operator">+</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_execute</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">int</span> rarg_sht<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// run builtin command</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> WISH_BUILTIN_NUM<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strcmp</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> builtin_str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token operator">*</span>builtin_func<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">wish_launch</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> rarg_sht<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_launch</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">int</span> rarg_sht<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// search path</span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>executable_path <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> WISH_MAX_FNAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> WISH_MAX_FNAME<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span>
        executable_path<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> temp_path <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> WISH_MAX_FNAME<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// copy path[i] to temp and then concate</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strncpy</span><span class="token punctuation">(</span>temp_path<span class="token punctuation">,</span> path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token constant">__LINE__</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> WISH_EXIT_FAILURE<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token function">strlen</span><span class="token punctuation">(</span>temp_path<span class="token punctuation">)</span><span class="token punctuation">;</span>
        temp_path<span class="token punctuation">[</span>len<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'/'</span><span class="token punctuation">;</span>
        temp_path<span class="token punctuation">[</span>len <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strcat</span><span class="token punctuation">(</span>temp_path<span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token constant">__LINE__</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> WISH_EXIT_FAILURE<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">// check privilege</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">access</span><span class="token punctuation">(</span>temp_path<span class="token punctuation">,</span> X_OK<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strcpy</span><span class="token punctuation">(</span>executable_path<span class="token punctuation">,</span> temp_path<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token constant">__LINE__</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> WISH_EXIT_FAILURE<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        i<span class="token operator">++</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">free</span><span class="token punctuation">(</span>temp_path<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">// print error if not found</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>executable_path<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">'\0'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">free</span><span class="token punctuation">(</span>executable_path<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token constant">__LINE__</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> EXIT_FAILURE<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> status<span class="token punctuation">;</span>
    pid_t son_pid<span class="token punctuation">,</span> wait_pid<span class="token punctuation">;</span>
    son_pid <span class="token operator">=</span> <span class="token function">fork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>son_pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// child process</span>
        <span class="token comment" spellcheck="true">// redirection</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span> <span class="token operator">!=</span> rarg_sht <span class="token operator">&amp;&amp;</span> <span class="token constant">NULL</span> <span class="token operator">!=</span> args<span class="token punctuation">[</span>rarg_sht<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">// get real path</span>
            <span class="token keyword">char</span> rp<span class="token punctuation">[</span>WISH_MAX_FNAME<span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token function">realpath</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span>rarg_sht<span class="token punctuation">]</span><span class="token punctuation">,</span> rp<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> <span class="token function">freopen</span><span class="token punctuation">(</span>rp<span class="token punctuation">,</span> <span class="token string">"w"</span><span class="token punctuation">,</span> <span class="token constant">stdout</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">"wish: redirection file %s open fail!\n"</span><span class="token punctuation">,</span> rp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">// run cmd</span>
        <span class="token keyword">int</span> <span class="token punctuation">(</span><span class="token operator">*</span>func<span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>WISH_MY_SEARCH <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span>
            func <span class="token operator">=</span> execvp<span class="token punctuation">;</span>
        <span class="token keyword">else</span>
            func <span class="token operator">=</span> execv<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> args<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">// wish_error();</span>
            <span class="token comment" spellcheck="true">// wish_line(__LINE__);</span>
            <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">// if run the following code, then it is wrong</span>
        <span class="token function">exit</span><span class="token punctuation">(</span>WISH_EXIT_FAILURE<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>son_pid <span class="token operator">&lt;</span> <span class="token number">0</span><span class="token punctuation">)</span> 
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">"wish: son process create fail by fork"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">do</span> <span class="token punctuation">{</span> 
            wait_pid <span class="token operator">=</span> <span class="token function">waitpid</span><span class="token punctuation">(</span>son_pid<span class="token punctuation">,</span> <span class="token operator">&amp;</span>status<span class="token punctuation">,</span> WUNTRACED<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">WIFEXITED</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token operator">!</span><span class="token function">WIFSIGNALED</span><span class="token punctuation">(</span>status<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> true<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">char</span> <span class="token operator">*</span><span class="token function">wish_search</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>cmd<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">char</span> <span class="token operator">*</span>err_msg <span class="token operator">=</span> <span class="token string">"An error has occurred\n"</span><span class="token punctuation">;</span>
    <span class="token function">write</span><span class="token punctuation">(</span>STDERR_FILENO<span class="token punctuation">,</span> err_msg<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>err_msg<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">void</span> <span class="token function">wish_line</span><span class="token punctuation">(</span><span class="token keyword">int</span> lineno<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token macro property">#<span class="token directive keyword">ifdef</span> WISH_DEBUG</span>
        <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">"in line %d\n"</span><span class="token punctuation">,</span> lineno<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">#<span class="token directive keyword">endif</span></span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_cd</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">chdir</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">0</span><span class="token punctuation">)</span>
            <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_exit</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">wish_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_path</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>path<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> args<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span>
        i<span class="token operator">++</span><span class="token punctuation">;</span>    
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_env</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token keyword">char</span> <span class="token operator">*</span> env <span class="token operator">=</span> <span class="token function">getenv</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s:\n"</span><span class="token punctuation">,</span> args<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%s\n"</span><span class="token punctuation">,</span> env<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">wish_help</span><span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span>args<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"WISH written by Shihong Wang.\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Usage: command  argument [enter]\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"Builtin commands:\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> WISH_BUILTIN_NUM<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\t%s"</span><span class="token punctuation">,</span> builtin_str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\nRefer man page of other command.\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h4 id="3-main-c"><a href="#3-main-c" class="headerlink" title="3 ) main.c"></a>3 ) main.c</h4><pre class="line-numbers language-c"><code class="language-c"><span class="token macro property">#<span class="token directive keyword">include</span> <span class="token string">"wish.h"</span></span>

<span class="token keyword">extern</span> <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span>environ<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>argc <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// command loop mode</span>
        <span class="token function">wish_loop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">// read-parse-execute mode</span>
        FILE <span class="token operator">*</span>file<span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">==</span> <span class="token punctuation">(</span>file <span class="token operator">=</span> <span class="token function">fopen</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">"r"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">fprintf</span><span class="token punctuation">(</span><span class="token constant">stderr</span><span class="token punctuation">,</span> <span class="token string">"wish: read-parse mode %s file not exists!\n"</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">exit</span><span class="token punctuation">(</span>WISH_EXIT_FAILURE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">// read a line, parse and execute</span>
        <span class="token keyword">int</span> status<span class="token punctuation">;</span>
        <span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">*</span> args<span class="token punctuation">;</span>
        size_t len <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
        ssize_t read<span class="token punctuation">;</span>
        <span class="token keyword">char</span> <span class="token operator">*</span>line <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token punctuation">)</span> <span class="token function">malloc</span><span class="token punctuation">(</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token operator">*</span> WISH_BUF_SIZE<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>read <span class="token operator">=</span> <span class="token function">getline</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>line<span class="token punctuation">,</span> <span class="token operator">&amp;</span>len<span class="token punctuation">,</span> file<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>line<span class="token punctuation">[</span><span class="token operator">++</span>j<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            line<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">;</span>
            args <span class="token operator">=</span> <span class="token function">wish_split_line</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>i <span class="token operator">=</span> <span class="token function">wish_redirection</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                args<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            status <span class="token operator">=</span> <span class="token function">wish_execute</span><span class="token punctuation">(</span>args<span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> WISH_MAX_WORD<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span>
                    <span class="token function">free</span><span class="token punctuation">(</span>args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token function">free</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">free</span><span class="token punctuation">(</span>line<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> WISH_EXIT_SUCCESS<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="B-tasks-json的内容"><a href="#B-tasks-json的内容" class="headerlink" title="B. tasks.json的内容"></a>B. tasks.json的内容</h3><p>我们在调试前，需要：</p>
<ul>
<li>创建<code>build</code>文件夹</li>
<li>进入<code>build</code>文件夹使用<code>cmake</code>配置项目</li>
<li>使用<code>make</code>或者<code>cmake --build ./ --target all</code>进行编译</li>
</ul>
<p>因此，我们需要再<code>tasks.json</code>中定义三个任务</p>
<pre class="line-numbers language-json"><code class="language-json">    <span class="token punctuation">{</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"create_build"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"mkdir"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-p"</span><span class="token punctuation">,</span>
                <span class="token string">"${workspaceFolder}/build"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"detail"</span><span class="token operator">:</span> <span class="token string">"创建build文件夹"</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"cmake_configure"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"cmake"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build"</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}"</span><span class="token punctuation">,</span>
                <span class="token string">"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"</span><span class="token punctuation">,</span> // 生成compile_commands.json 供c/c++扩展提示使用
                <span class="token string">"../"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"dependsOn"</span><span class="token operator">:</span> <span class="token string">"create_build"</span><span class="token punctuation">,</span>
            <span class="token property">"detail"</span><span class="token operator">:</span> <span class="token string">"CMake配置项目"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"make_build"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"make"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build"</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"all"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"dependsOn"</span><span class="token operator">:</span> <span class="token string">"cmake_configure"</span><span class="token punctuation">,</span>
            <span class="token property">"detail"</span><span class="token operator">:</span> <span class="token string">"Make构建项目"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>

    <span class="token property">"inputs"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"CMAKE_BUILD_TYPE"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"pickString"</span><span class="token punctuation">,</span>
            <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"选择项目的编译类型(CMake Build Type)"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"Debug"</span><span class="token punctuation">,</span>
                <span class="token string">"Release"</span><span class="token punctuation">,</span>
                <span class="token string">"RelWithDebInfo"</span><span class="token punctuation">,</span>
                <span class="token string">"MinSizeRel"</span><span class="token punctuation">,</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"default"</span><span class="token operator">:</span> <span class="token string">"Debug"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>关于<code>input</code>标签，参考手册的这一节：<a target="_blank" rel="noopener" href="https://code.visualstudio.com/docs/editor/variables-reference">https://code.visualstudio.com/docs/editor/variables-reference</a></p>
<h3 id="C-launch-json的内容"><a href="#C-launch-json的内容" class="headerlink" title="C. launch.json的内容"></a>C. launch.json的内容</h3><p><code>launch.json</code>的内容如下</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">{</span>
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息，请访问: https://go.microsoft.com/fwlink/?linkid<span class="token operator">=</span>830387
    <span class="token string">"version"</span><span class="token keyword">:</span> <span class="token string">"0.2.0"</span>,
    <span class="token string">"configurations"</span><span class="token keyword">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token string">"type"</span><span class="token keyword">:</span> <span class="token string">"cppdbg"</span>,
            <span class="token string">"request"</span><span class="token keyword">:</span> <span class="token string">"launch"</span>,
            <span class="token string">"name"</span><span class="token keyword">:</span> <span class="token string">"LLDB Debug"</span>,
            <span class="token string">"program"</span><span class="token keyword">:</span> <span class="token string">"<span class="token variable">${workspaceFolder}</span>/bin/wish"</span>,
            <span class="token string">"stopAtEntry"</span><span class="token keyword">:</span> true,
            <span class="token string">"preLaunchTask"</span><span class="token keyword">:</span> <span class="token string">"make_build"</span>,
            <span class="token string">"cwd"</span><span class="token keyword">:</span> <span class="token string">"<span class="token variable">${workspaceFolder}</span>"</span>,
            <span class="token string">"MIMode"</span><span class="token keyword">:</span> <span class="token string">"lldb"</span>,
        <span class="token punctuation">}</span>,
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="D-开始调试-1"><a href="#D-开始调试-1" class="headerlink" title="D. 开始调试"></a>D. 开始调试</h3><p>按下<code>F5</code>开始调试</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/%E5%B1%8F%E5%B9%95%E5%BD%95%E5%88%B62022-10-11-%E4%B8%8B%E5%8D%8811.28.15%20(1" alt="开始调试">.gif)</p>
<h2 id="9-CMake工程常用的tasks-json和launch-json"><a href="#9-CMake工程常用的tasks-json和launch-json" class="headerlink" title="9. CMake工程常用的tasks.json和launch.json"></a>9. CMake工程常用的tasks.json和launch.json</h2><p>下面给出一个<code>CMake</code>工程常用的<code>tasks.json</code>和<code>launch.json</code></p>
<pre class="line-numbers language-json"><code class="language-json">// tasks.json
<span class="token punctuation">{</span>
    // See https<span class="token operator">:</span>//go.microsoft.com/fwlink/?LinkId=<span class="token number">733558</span>
    // for the documentation about the tasks.json format
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"2.0.0"</span><span class="token punctuation">,</span>
    <span class="token property">"tasks"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span> 
            // 在根文件夹中执行创建文件夹build的命令
            // 除windows系统外执行的命令为`mkdir -p build`
            // windows系统是在powershell中执行命令`mkdir -Force build`
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"build_dir"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"mkdir"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-p"</span><span class="token punctuation">,</span>
                <span class="token string">"build"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"shell"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                        <span class="token property">"executable"</span><span class="token operator">:</span> <span class="token string">"powershell.exe"</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token string">"-Force"</span><span class="token punctuation">,</span>
                    <span class="token string">"build"</span>
                <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            // 在build文件夹中调用cmake进行项目配置
            // 除windows系统外执行的命令为`cmake -DCMAKE_BUILD_TYPE=&lt;Debug|Release|RelWithDebInfo|MinSizeRel> ../`
            // windows系统是在visual stuido的环境中执行命令`cmake -DCMAKE_BUILD_TYPE=&lt;Debug|Release|RelWithDebInfo|MinSizeRel>  ../ -G <span class="token string">"CodeBlocks - NMake Makefiles"</span>`
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"cmake"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"cmake"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}"</span><span class="token punctuation">,</span>
                <span class="token string">"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"</span><span class="token punctuation">,</span> // 生成compile_commands.json 供c/c++扩展提示使用
                <span class="token string">"../"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build"</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token string">"-DCMAKE_BUILD_TYPE=${input:CMAKE_BUILD_TYPE}"</span><span class="token punctuation">,</span>
                    <span class="token string">"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON"</span><span class="token punctuation">,</span>
                    <span class="token string">"../"</span><span class="token punctuation">,</span>
                    <span class="token string">"-G"</span><span class="token punctuation">,</span>
                    <span class="token string">"\"CodeBlocks - NMake Makefiles\""</span>
                <span class="token punctuation">]</span><span class="token punctuation">,</span>
                <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"shell"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                        // 需要根据安装的vs版本调用vs工具命令提示符，根据自己的计算机上的路径进行修改
                        <span class="token property">"executable"</span><span class="token operator">:</span> <span class="token string">"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat"</span><span class="token punctuation">,</span>
                        <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                            <span class="token string">"${input:PLATFORM}"</span><span class="token punctuation">,</span> //指定平台
                            <span class="token string">"-vcvars_ver=${input:vcvars_ver}"</span><span class="token punctuation">,</span> //指定vc环境版本
                            <span class="token string">"&amp;&amp;"</span>
                        <span class="token punctuation">]</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"dependsOn"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"build_dir"</span> // 在task `build_dir` 后执行该task
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            // 在build文件夹中调用cmake编译构建debug程序
            // 执行的命令为`cmake --build ./ --target all --`
            //  windows系统如上需要在visual stuido的环境中执行命令
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"build"</span><span class="token punctuation">,</span>
            <span class="token property">"group"</span><span class="token operator">:</span> <span class="token string">"build"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"cmake"</span><span class="token punctuation">,</span>
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"--build"</span><span class="token punctuation">,</span>
                <span class="token string">"./"</span><span class="token punctuation">,</span>
                <span class="token string">"--target"</span><span class="token punctuation">,</span>
                <span class="token string">"all"</span><span class="token punctuation">,</span>
                <span class="token string">"--"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build"</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"problemMatcher"</span><span class="token operator">:</span> <span class="token string">"$gcc"</span><span class="token punctuation">,</span>
            <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">"shell"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                        <span class="token property">"executable"</span><span class="token operator">:</span> <span class="token string">"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvarsall.bat"</span><span class="token punctuation">,</span>
                        <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                            <span class="token string">"${input:PLATFORM}"</span><span class="token punctuation">,</span>
                            <span class="token string">"-vcvars_ver=${input:vcvars_ver}"</span><span class="token punctuation">,</span>
                            <span class="token string">"&amp;&amp;"</span>
                        <span class="token punctuation">]</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
                <span class="token property">"problemMatcher"</span><span class="token operator">:</span> <span class="token string">"$msCompile"</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token property">"dependsOn"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"cmake"</span> // 在task `cmake` 后执行该task
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"label"</span><span class="token operator">:</span> <span class="token string">"Open Terminal"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"shell"</span><span class="token punctuation">,</span>
            <span class="token property">"command"</span><span class="token operator">:</span> <span class="token string">"osascript -e 'tell application \"Terminal\"\ndo script \"echo hello\"\nend tell'"</span><span class="token punctuation">,</span>
            <span class="token property">"problemMatcher"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"inputs"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"CMAKE_BUILD_TYPE"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"pickString"</span><span class="token punctuation">,</span>
            <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"指定 CMAKE_BUILD_TYPE 的值"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"Debug"</span><span class="token punctuation">,</span>
                <span class="token string">"Release"</span><span class="token punctuation">,</span>
                <span class="token string">"RelWithDebInfo"</span><span class="token punctuation">,</span>
                <span class="token string">"MinSizeRel"</span><span class="token punctuation">,</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"default"</span><span class="token operator">:</span> <span class="token string">"Debug"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"PLATFORM"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"pickString"</span><span class="token punctuation">,</span>
            <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"指定 PLATFORM 的值"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"x86"</span><span class="token punctuation">,</span>
                <span class="token string">"amd64"</span><span class="token punctuation">,</span>
                <span class="token string">"arm"</span><span class="token punctuation">,</span>
                <span class="token string">"x86_arm"</span><span class="token punctuation">,</span>
                <span class="token string">"x86_amd64"</span><span class="token punctuation">,</span>
                <span class="token string">"amd64_x86"</span><span class="token punctuation">,</span>
                <span class="token string">"amd64_arm"</span><span class="token punctuation">,</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"default"</span><span class="token operator">:</span> <span class="token string">"amd64"</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"vcvars_ver"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"pickString"</span><span class="token punctuation">,</span>
            <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"指定 vcvars_ver 的值"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"14.2"</span><span class="token punctuation">,</span> // <span class="token number">2019</span>
                <span class="token string">"14.1"</span><span class="token punctuation">,</span> // <span class="token number">2017</span>
                <span class="token string">"14.0"</span><span class="token punctuation">,</span> // <span class="token number">2015</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"default"</span><span class="token operator">:</span> <span class="token string">"14.2"</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>注意，如果是需要以<code>Attach Debug</code>方式启动的调试的话，运行中的进程在编译的时候必须要加上<code>-g</code>以将符号表写入到程序中，从而能够<code>debug</code>程序，若使用<code>CMake</code>工具的话，需要指定使用<code>Debug</code>方式来构建程序，而非<code>MinSizeRel</code>等其他构建方式</p>
<pre class="line-numbers language-json"><code class="language-json">// launch.json
<span class="token punctuation">{</span>
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information<span class="token punctuation">,</span> visit<span class="token operator">:</span> https<span class="token operator">:</span>//go.microsoft.com/fwlink/?linkid=<span class="token number">830387</span>
    <span class="token property">"version"</span><span class="token operator">:</span> <span class="token string">"0.2.0"</span><span class="token punctuation">,</span>
    <span class="token property">"configurations"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            //名称
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Launch Debug"</span><span class="token punctuation">,</span>
            //调试类型，除使用msvc进行调试外，均为该类型
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"cppdbg"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"launch"</span><span class="token punctuation">,</span>
            //指定C/C++程序位置
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/bin/${input:executable}"</span><span class="token punctuation">,</span>
            //指定运行参数
            <span class="token property">"args"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"test.bin"</span><span class="token punctuation">,</span>
                <span class="token string">"sorted.bin"</span>
            <span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token property">"stopAtEntry"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            //指定工作目录
            <span class="token property">"cwd"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}"</span><span class="token punctuation">,</span>
            //在调试前会先调用build_debug这个task编译构建程序
            <span class="token property">"preLaunchTask"</span><span class="token operator">:</span> <span class="token string">"build"</span><span class="token punctuation">,</span>
            <span class="token property">"environment"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            //macOS的特定配置
            <span class="token property">"osx"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用lldb进行调试
                <span class="token property">"MIMode"</span><span class="token operator">:</span> <span class="token string">"lldb"</span><span class="token punctuation">,</span>
                // 使用外部终端
                <span class="token property">"externalConsole"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            //linux的特定配置
            <span class="token property">"linux"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用gdb调试
                <span class="token property">"MIMode"</span><span class="token operator">:</span> <span class="token string">"gdb"</span><span class="token punctuation">,</span>
                <span class="token property">"setupCommands"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token punctuation">{</span>
                        <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"Enable pretty-printing for gdb"</span><span class="token punctuation">,</span>
                        <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"-enable-pretty-printing"</span><span class="token punctuation">,</span>
                        <span class="token property">"ignoreFailures"</span><span class="token operator">:</span> <span class="token boolean">true</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            //windows的特定配置
            <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用msvc进行调试
                <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"cppdbg"</span><span class="token punctuation">,</span>
                //指定C/C++程序位置
                <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build/${workspaceFolderBasename}.exe"</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            //名称
            <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Attach Debug"</span><span class="token punctuation">,</span>
            //调试类型，除使用msvc进行调试外，均为该类型
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"cppdbg"</span><span class="token punctuation">,</span>
            <span class="token property">"request"</span><span class="token operator">:</span> <span class="token string">"attach"</span><span class="token punctuation">,</span>
            //指定C/C++程序位置
            <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/bin/${input:executable}"</span><span class="token punctuation">,</span>
            //指定要attach的线程
            <span class="token property">"processId"</span><span class="token operator">:</span><span class="token string">"${command:pickProcess}"</span><span class="token punctuation">,</span>
            <span class="token property">"osx"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用lldb进行调试
                <span class="token property">"MIMode"</span><span class="token operator">:</span> <span class="token string">"lldb"</span><span class="token punctuation">,</span>
                // 使用外部终端
                <span class="token property">"externalConsole"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            //linux的特定配置
            <span class="token property">"linux"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用gdb调试
                <span class="token property">"MIMode"</span><span class="token operator">:</span> <span class="token string">"gdb"</span><span class="token punctuation">,</span>
                <span class="token property">"setupCommands"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                    <span class="token punctuation">{</span>
                        <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"Enable pretty-printing for gdb"</span><span class="token punctuation">,</span>
                        <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"-enable-pretty-printing"</span><span class="token punctuation">,</span>
                        <span class="token property">"ignoreFailures"</span><span class="token operator">:</span> <span class="token boolean">true</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">]</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            //windows的特定配置
            <span class="token property">"windows"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                //指定使用msvc进行调试
                <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"cppdbg"</span><span class="token punctuation">,</span>
                //指定C/C++程序位置
                <span class="token property">"program"</span><span class="token operator">:</span> <span class="token string">"${workspaceFolder}/build/${workspaceFolderBasename}.exe"</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token property">"inputs"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"executable"</span><span class="token punctuation">,</span>
            <span class="token property">"type"</span><span class="token operator">:</span> <span class="token string">"pickString"</span><span class="token punctuation">,</span>
            <span class="token property">"description"</span><span class="token operator">:</span> <span class="token string">"可执行文件的名称"</span><span class="token punctuation">,</span>
            <span class="token property">"default"</span><span class="token operator">:</span> <span class="token string">"posrt"</span><span class="token punctuation">,</span>
            <span class="token property">"options"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
                <span class="token string">"psort"</span>
            <span class="token punctuation">]</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2022/10/11/vscode-diao-shi-c-c-pei-zhi-tasks-json-he-launch-json/">https://jackwang0107.github.io/2022/10/11/vscode-diao-shi-c-c-pei-zhi-tasks-json-he-launch-json/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/%E6%9D%82%E9%A1%B9/">
                                    <span class="chip bg-color">杂项</span>
                                </a>
                            
                                <a href="/tags/VSCode/">
                                    <span class="chip bg-color">VSCode</span>
                                </a>
                            
                                <a href="/tags/tasks-json/">
                                    <span class="chip bg-color">tasks.json</span>
                                </a>
                            
                                <a href="/tags/launch-json/">
                                    <span class="chip bg-color">launch.json</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/10/12/gams-jiao-cheng-1-shu-ju-lei-xing-yi-ji-cheng-xu-jie-gou/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/image-20221012223017100.png" class="responsive-img" alt="GAMS教程 1：数据类型及程序结构">
                        
                        <span class="card-title">GAMS教程 1：数据类型及程序结构</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文介绍了GAMS的数据类型及程序结构
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-10-12
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/GAMS/" class="post-category">
                                    GAMS
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/GAMS/">
                        <span class="chip bg-color">GAMS</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/10/01/method-xue-xi-bi-ji/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/23.jpg" class="responsive-img" alt="Method学习笔记">
                        
                        <span class="card-title">Method学习笔记</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本文是Method的学习笔记，记录了我的个人学习心得
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-10-01
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E4%B8%93%E9%A1%B9%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" class="post-category">
                                    专项学习笔记
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">
                        <span class="chip bg-color">学习笔记</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
